<?php

/**
 *
 *
 * @version $Id$
 * @copyright 2011
 */
require_once('classes\class.Reflector.php');
require_once('classes\class.String.php');
require_once('classes\class.Parser.php');
require_once('classes\class.Property.php');

class Person extends Property {
	/* define variables parameters*/
	private $name = '';
	private $surname = '';
	private $address = '';
	private $filename = '';
	private $ages = 0;

	/* constructor */
	public function __construct($name = '', $surname = '', $address= '', $ages = 0){
		$this->name = $name;
		$this->surname = $surname;
		$this->address = $address;
		$this->ages = $ages;
		$this->properties = array(
			"Name" => NULL,
			"Email" => NULL,
			"Address" => NULL,
			"Surname" => NULL
		);
	}

	/* method getName for display name */
	public function getName(){
		return ($this->name);
	}

	/* method getSurname for display surname */
	public function getSurname(){
		return ($this->surname);
	}

	/* method getAge for display age */
	public function getAge(){
		return ($this->ages);
	}

	/* method getAddress for display address */
	public function getAddress(){
		return ($this->address);
	}

	/* method setName for set value of name */
	public function setName($name = ''){
		$this->name = $name;
	}

	/* method setSurname for set value of surname */
	public function setSurname($surname = ''){
		$this->surname = $surname;
	}

	/* method setAge for set value of age */
	public function setAge($age = 0){
		$this->ages = $age;
	}

	/* method setAddress for set value of address */
	public function setAddress($address = ''){
		$this->address = $address;
	}

	/*
		method Init for clear value of class person
	*/

	public function Init(){
		$this->name = '';
		$this->surname = '';
		$this->address = '';
		$this->ages = 0;
	}

	/*
		 method Save for save data into file
	     option 1 is append file
		 option 0 is write new file
	*/
	public function save($filename = "", $object = NULL, $option = TRUE) {
		$op = ($option) ? "a+" : "w";
	    if(trim($filename) != ""){
			$fp = @fopen($filename, $op);
			if($fp == NULL) {
				return false;
			}
			else{
				fwrite($fp, serialize($object) . "\n");
	            fclose($fp);
				unset($fp);
			    return true;
			}
		}
		else {
			return false;
		}
	}

	/* method dump show data */
	function restore($filename='') {
		$object = NULL;
		if(trim($filename) != ''){
			$fp = @fopen($filename, "r");
			if($fp !== NULL) {
				if(!feof($fp))
					$object = unserialize(fgets($fp));
				fclose($fp);
				unset($fp);
			}
		}

		return $object;
	}
}

$person = new Person("Tran Hung", "Hoang Tran Hung", "So 11, Nguyen Trai, Sala - Ha Dong", 45);
$reflector = new RadReflector($person);
echo("MyName=" . $reflector->invoke("getName", NULL));
echo("<br />MyName=" . RadReflector::invokeMethod($person, "getName", NULL));
echo("<br />MySurname=" . $person->Surname . "<br />");

function getFirstValidCommaPos($text) {
		$pos = strpos($text, ",");
		$subPos = strpos($text, "\\");
		while(($pos === $subPos + 1) && ($pos > 0)) {
			$pos = strpos($text, ",", $pos + 1);
			if ($pos !== FALSE)
				$subPos = strpos($text, "\\", $pos - 1);
		}

		return $pos;
	}

function stripBackSlash($text) {
		$pos = strpos($text, ",");
		$subPos = strpos($text, "\\");
		while(($pos === $subPos + 1) && ($pos > 0)) {
			$text = substr($text, 0, $subPos) . substr($text, $subPos + 1);
			$subPos = strpos($text, "\\");
			$pos = strpos($text, ",", $subPos);
		}

		return $text;
}

	function parseMethodArgs($text) {
		$arg = array();
		$args = array();
		$len = strlen($text);
		$i = 0;
		$pos = 0;
		while($len > 0) {
			$pos = getFirstValidCommaPos($text);
			if(String::startsWith($text, "$")) {
				if ($pos > 0) {
					$arg["var"] = TRUE;
					$arg["value"] = substr($text, 1, $pos);
					$args[$i] = $arg;
					$text = trim(substr($text, $pos + 1));
				}
				else {
					$arg["var"] = TRUE;
					$arg["value"] = trim(substr($text, 1));
					$args[$i] = $arg;
					$text = "";
				}
			}
			else {
				if ($pos > 0) {
					$arg["var"] = FALSE;
					$arg["value"] = stripBackSlash(trim(substr($text, 0, $pos)));
					$args[$i] = $arg;
					$text = substr($text, $pos + 1);
					$i++;
				}
				else {
					$arg["var"] = FALSE;
					$arg["value"] = stripBackSlash(trim($text));
					$args[$i] = $arg;
					$text = "";
				}
			}

			$len = strlen($text);
			$i++;
		}

		return $args;
	}

	$s = "\" My\, Love\,, My Life\,, \,My Darling\,, \$starLife";

	$args = parseMethodArgs($s);
	foreach($args as $arg) {
		echo("<br />Arg=");
		print_r($arg);
	}

	$s = "obj->Engine->getObject(\$name, 25, \$email, 26 tinhte)->getObjectName()->Name";

	$data = TemplateObjectParser::parse($s);
	echo("<br />Parsed Method Call<br /><br />");
	foreach($data["trail"] as $callee) {
		var_dump($callee);
		echo("<br /><br />");
	}

	$s= "I love you";
	echo("<br /><br />Test=" . $s[0]);
?>